package com.sitech.portal.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.sitech.ibnms.core.IPage;
import com.sitech.ibnms.core.PageForm;
import com.sitech.ibnms.util.JSONUtil;
import com.sitech.portal.system.dao.ISysLogDao;
import com.sitech.portal.system.service.ISysLogService;

public class SysLogServiceImpl implements ISysLogService {

	private ISysLogDao sysLogDao;
	
	public ISysLogDao getSysLogDao() {
		return sysLogDao;
	}

	public void setSysLogDao(ISysLogDao sysLogDao) {
		this.sysLogDao = sysLogDao;
	}

	@Override
	public void add(Map map) {
		// TODO Auto-generated method stub
		sysLogDao.add(map);
	}

	@Override
	public IPage findListByPK(String userId, PageForm pageForm) {
		IPage result = sysLogDao.findListByPK(userId, pageForm);
//		List list = (ArrayList)result.getRows();
//    	for(int i=0;i<list.size();i++){
//    		HashMap map = (HashMap)list.get(i);
//    		String beforeOptData = map.get("RAW_DATA")==null?null:map.get("RAW_DATA").toString();
//    		String afterOptData = map.get("OPERATE_ARGS")==null?null:map.get("OPERATE_ARGS").toString();
//    		String expression = map.get("TITLE_EXPRESSION")==null?null:map.get("TITLE_EXPRESSION").toString();
//    		if(expression!=null && expression.length()>0){
//	    		if(afterOptData!=null && !afterOptData.equals(""))
//	    			expression = this.formateJsonData(expression, afterOptData, "after.");
//	    		if(beforeOptData!=null && !beforeOptData.equals(""))
//	    			expression = this.formateJsonData(expression, beforeOptData, "before.");
//	    		map.put("TITLE_EXPRESSION", expression);
//    		}
//    	}
    	
    	return result;
	}
	
	public IPage findLogByPK(String id,PageForm pageForm){
		IPage result = sysLogDao.findLogByPK(id, pageForm);
		List list = (ArrayList)result.getRows();
    	for(int i=0;i<list.size();i++){
    		HashMap map = (HashMap)list.get(i);
    		String beforeOptData = map.get("RAW_DATA")==null?null:map.get("RAW_DATA").toString();
    		String afterOptData = map.get("OPERATE_ARGS")==null?null:map.get("OPERATE_ARGS").toString();
    		String expression = map.get("TITLE_EXPRESSION")==null?null:map.get("TITLE_EXPRESSION").toString();
    		if(expression!=null && expression.length()>0){
	    		if(afterOptData!=null && !afterOptData.equals(""))
	    			expression = this.formateJsonData(expression, afterOptData, "after.");
	    		if(beforeOptData!=null && !beforeOptData.equals(""))
	    			expression = this.formateJsonData(expression, beforeOptData, "before.");
	    		map.put("TITLE_EXPRESSION", expression);
    		}
    	}
    	
    	return result;
		
	}
	
	private String formateJsonData(String expression , String jsonStr, String flag){
		List listData = (List)JSONUtil.fromJSON(jsonStr);
		if(listData!=null && listData.size()>0){
			if(listData.get(0) instanceof HashMap){
	    		HashMap dataMap = (HashMap)listData.get(0);
				Iterator iter = dataMap.entrySet().iterator();
				while (iter.hasNext()) {
					try {
	    				Map.Entry entry = (Map.Entry) iter.next();
	    				String key = entry.getKey().toString();
	    				Object val = entry.getValue();
	    				if(val!=null)
	    					expression = expression.replaceAll(flag+key, val.toString());
					} catch (Exception e) {
		    			e.printStackTrace();
		    			return null;
		    		}
				}
			}
    	}
		return expression;
	}

	@Override
	public List findUserList(String dept_id) {
		// TODO Auto-generated method stub
		return sysLogDao.findUserList(dept_id);
	}

	@Override
	public List getAllModuleForLogin(String loginId, String pre_super,
			int pre_super_len) {
		// TODO Auto-generated method stub
		return sysLogDao.getAllModuleForLogin(loginId, pre_super, pre_super_len);
	}

	@Override
	public HashMap getModuleInfoByPk(String module_id) {
		// TODO Auto-generated method stub
		return sysLogDao.getModuleInfoByPk(module_id);
	}

}
